var g, app = getApp(), page = app.page, request = app.util.request, route = app.route, toast = app.toast;

function getList() {
    let sort = ''
    switch(g.data.cur){
      case 0 :
        sort = 'money'
      break;
      case 1 :
        sort = 'num'
      break;
    }
    request({
        url: "rankList",
        data: {
            user_id: app.userInfo.id || wx.getStorageSync("userInfo").id,
            date: g.data.date + (g.data.day > 0 ? ('-'+g.data.days[g.data.day]) : ''),
            page:  g.data.page,
            type: g.data.day > 0 ? 'day' : 'month',
            number: 10,
            sort 
        },
        success: function(t) {
          let list = g.data.list
          if(g.data.page == 1){
            list = t
          } else {
            list = list.concat(t)
          }
          g.setData({
            list
          })
          wx.stopPullDownRefresh()
        }
    });
}

page({
    data: {
        cur: 0,
        no_orders: app.srcRoot + "no_order.png",
        items: [ "业绩排行榜", "销量排行榜"],
        page: 1,
        list: [],
        date: "",
        days: [],
        day: ''
    },
    onLoad: function(t) {
      let date = new Date(),
      year = date.getFullYear(),
      month = date.getMonth() + 1, 
      strDate = date.getDate()
      if (month < 10) month = `0${month}` // 如果月份是个位数，在前面补0
      if (strDate < 10) strDate = `0${strDate}` // 如果日是个位数，在前面补0
      let days= ['全部']
      for(let i = 1;i <= 31;i++ ){
        days.push(i < 10 ? ('0'+i) : i)
      }
      g = this
      g.setData({
        date: year + "-" + month,
        days
      })
      getList()
    },
    bindDayChange: function(e) {
      g.setData({
        day: e.detail.value,
        page: 1
      })
      getList()
    },
    bindDateChange: function(e) {
      g.setData({
        date: e.detail.value,
        page: 1
      })
      getList()
    },
    getMore(){
      // g.setData({
      //   page: g.data.page + 1
      // })
      // getList()
    },
    onPullDownRefresh: function() {
      this.setData({
        page: 1
      });
      getList();
    },
    tabSelect: function(t) {
        this.setData({
            cur: t.currentTarget.dataset.index,
            page: 1
        });
        getList();
    },
    keywordSearch: function(){
      getList()
    },
    input: function(a) {
      let keyword = a.detail.value;
      g.setData({
        keyword
      })	
    },
});